//
//  GoodsDetailHeadView.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/24.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "GoodsDetailHeadView.h"
#import "GoodsSelectVC.h"

@interface GoodsDetailHeadView ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) SDCycleScrollView *bannerView; //轮播图
@property (nonatomic, strong) UIPageControl *pageControl;      //分页指示器
@property (nonatomic, strong) UILabel *priceLabel;                  //现售价
@property (nonatomic, strong) UILabel *oldPriceLabel;              //初上市价格
@end

@implementation GoodsDetailHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    //轮播图
    [self addSubview:self.bannerView];
    [_bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.width.equalTo(self.bannerView.mas_height);
    }];
    //分页指示器
    [self addSubview:self.pageControl];
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_bannerView.mas_bottom);
        make.height.mas_equalTo(kAppWidth_Mini(80));
    }];
    //现售价
    [self addSubview:self.priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kAppWidth_Mini(120));
        make.left.mas_equalTo(kAppWidth_Mini(50));
        make.top.equalTo(_pageControl.mas_bottom);
    }];
    //初上市价格
    [self addSubview:self.oldPriceLabel];
    [_oldPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kAppWidth_Mini(50));
        make.height.top.equalTo(_priceLabel);
    }];
    //分隔线
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kAppWidth_Mini(25));
        make.top.mas_equalTo(_priceLabel.mas_bottom);
    }];
    //按钮
    UIButton *selectButtton = [UIButton initWithTitleStr:@"请选择配送方式,颜色,尺码,数量" titleColor:[UIColor grayColor] withFont:UIPFThinFont(15)];
    selectButtton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [selectButtton addTarget:self action:@selector(selectAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:selectButtton];
    [selectButtton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kAppWidth_Mini(50));
        make.height.mas_equalTo(kAppWidth_Mini(175));
        make.top.equalTo(lineView.mas_bottom);
        make.right.mas_equalTo(-kAppWidth_Mini(50));
    }];
    //分隔线
    UIView *lineView1 = [UIView new];
    lineView1.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kAppWidth_Mini(25));
        make.top.mas_equalTo(selectButtton.mas_bottom);
    }];
}

#pragma mark - 懒加载
-(SDCycleScrollView *)bannerView{
    if (!_bannerView) {
        _bannerView = [[SDCycleScrollView alloc] init];
        _bannerView.showPageControl = NO;
        _bannerView.delegate = self;
    }
    return _bannerView;
}

-(UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    }
    return _pageControl;
}

-(UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [UILabel initWithTextColor:[UIColor orangeColor] fontStr:UIPFRegularFont(15)];
    }
    return _priceLabel;
}

-(UILabel *)oldPriceLabel{
    if (!_oldPriceLabel) {
        _oldPriceLabel = [UILabel initWithTextColor:[UIColor lightGrayColor] fontStr:UIPFRegularFont(13)];
    }
    return _oldPriceLabel;
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index;{
    self.pageControl.currentPage = index;
}

#pragma mark - 选择按钮点击
-(void)selectAction{
    GoodsSelectVC *vc = [GoodsSelectVC new];
    vc.detailModel = self.detailModel;
    kTabbarPushVC(vc);
}

#pragma mark - 赋值模块
-(void)setDetailModel:(GoodsDetailModel *)detailModel{
    _detailModel = detailModel;
    
    self.pageControl.numberOfPages = _detailModel.galleryList.count;
    self.bannerView.imageURLStringsGroup = [_detailModel.galleryList valueForKeyPath:@"allPathImageUrl"];
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",[AppTools getStringWithDouble:_detailModel.marketPrice]];
    self.oldPriceLabel.text = [NSString stringWithFormat:@"初上市价格  ¥%@",[AppTools getStringWithDouble:_detailModel.marketPrice]];
}


@end
